/**
 * 拦截 fetch 请求 
 */

// fetch arguments
const getFetchArg = (arg) => {
    let result={ method:'GET',type:"fetchrequest" }
    let args = Array.prototype.slice.apply(arg)

    if(!args || !args.length) return result;
    try{
        if(args.length === 1){
            if(typeof(args[0])==='string'){
                result.url      = args[0]
            }else if(typeof(args[0])==='object'){
                result.url      = args[0].url
                result.method   = args[0].method
            }
        }else{
            result.url      = args[0]
            result.method   = args[1].method
            result.type     = args[1].type
        }
    }catch(err){}
    return result;
}

export default (onready) => {
    if(!window.fetch) return;
    let _fetch     = fetch;
    window.fetch   = () => {
        let _arg   = arguments;
        let result = getFetchArg(_arg);
        if(typeof onready === "function") onready(result); 
        return _fetch.apply(this, arguments);
    }
}